﻿using Hotel.DAL;
using Hotel.Model;
using MD5Hash;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotel.BLL
{
    public class MemberBLL
    {
        public MemberBLL(MemberDAL memberDAL)
        {
            MemberDAL = memberDAL;
        }

        public MemberDAL MemberDAL { get; }


        /// <summary>
        /// 会员添加
        /// </summary>
        /// <param name="member"></param>
        /// <returns></returns>
        public int MemberAdd(Member member)
        {
            try
            {
                int res = MemberDAL.SearchPhoneCard(member.Phone,member.Card);
                if (res == -1)
                {
                    return -1;//手机号不能重复
                }
                else if (res==-2)
                {
                    return -2;//身份号不能重复
                }
                else
                {
                    return MemberDAL.MemberAdd(member);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 会员列表显示
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="levelId"></param>
        /// <returns></returns>
        public List<Member> GetMembers(int? id = null, string? name = "", int? levelId = null)
        {
            try
            {
                var res = MemberDAL.GetMembers(id,name,levelId);
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 会员等级添加
        /// </summary>
        /// <param name="memberLevel"></param>
        /// <returns></returns>
        public int MemberLevelAdd(MemberLevel memberLevel)
        {
            try
            {
                var res = MemberDAL.SearchLevelName(memberLevel.LevelName);
                if (res == -1)
                {
                    return -1;//会员等级名称不能重复
                }
                else
                {
                    int r = MemberDAL.MemberLevelAdd(memberLevel);
                    if (r > 0)
                    {
                        return 1;//添加成功
                    }
                    else
                    {
                        return -2;//添加失败
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 会员等级列表
        /// </summary>
        /// <returns></returns>
        public List<MemberLevel> GetMemberLevels()
        {
            try
            {
                var res = MemberDAL.GetMemberLevels();
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据id删除会员信息
        /// </summary>
        /// <param name="id">会员id</param>
        /// <returns>返回影响的行数</returns>
        public int DelMemberItem(int id)
        {
            try
            {
                int res = MemberDAL.DelMemberItem(id);
                if (res>0)
                {
                    return 1;//表示删除成功
                }
                else
                {
                    return -2;//表示删除失败
                }
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 根据会员id获取会员实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Member GetMemberInfo(int id)
        {
            try
            {
                var res = MemberDAL.GetMemberInfo(id);
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 修改会员
        /// </summary>
        /// <param name="member"></param>
        /// <returns></returns>
        public int UpdateMemberInfo(Member member)
        {
            try
            {
                int res = MemberDAL.UpdateMemberInfo(member);
                if (res>0)
                {
                    return 1;//修改成功
                }
                else
                {
                    return -2;//修改失败
                }
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 会员权益增加
        /// </summary>
        /// <param name="memberRight">权益实体</param>
        /// <returns></returns>
        public int MemberRightAdd(MemberRight memberRight)
        {
            try
            {
                //1.判断权益名称是否重复
                int res = MemberDAL.MemberRightName(memberRight.RightName);
                if (res>0)
                {
                    return -1;//表示重复
                }
                else
                {
                    //2.增加操作
                    int err = MemberDAL.MemberRightAdd(memberRight);
                    if (err>0)
                    {
                        return 1;//增加成功
                    }
                    else
                    {
                        return -2;//增加失败
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }



        /// <summary>
        /// 获取权益列表
        /// </summary>
        /// <param name="totalCount">总条数</param>
        /// <param name="totalPage">总页数</param>
        /// <param name="rightName">权益名称查询</param>
        /// <param name="createTime">创建时间查询</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns></returns>
        public List<MemberRight> MemberRightsList(out int totalCount, out int totalPage, string? rightName = "", DateTime? date1 = null, DateTime? date2 = null, int pageIndex = 1, int pageSize = 2)
        {
            try
            {
                //1.查询所有显示的数据
                List<MemberRight> list = MemberDAL.MemberRightsList(out totalCount,out totalPage, rightName, date1, date2, pageIndex, pageSize);

                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 会员权益逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelMemberRight(int id)
        {
            try
            {
                var res = MemberDAL.DelMemberRight(id);
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 会员权益反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public MemberRight GetMemberRight(int id)
        {
            try
            {
                var res = MemberDAL.GetMemberRight(id);
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 会员权益修改
        /// </summary>
        /// <param name="memberRight"></param>
        /// <returns></returns>
        public int GetMemberRightUpd(MemberRight memberRight)
        {
            try
            {
                var info = MemberDAL.GetMemberRightUpd(memberRight);
                return info;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据会员权益id修改状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public int UpdateMemberRightState(int id, int state)
        {
            try
            {
                //1.根据id找到对应的实体
                int info = MemberDAL.UpdateMemberRightState(id, state);
                if (info > 0)
                {
                    return 1;//表示修改成功
                }
                else
                {
                    return -2;//表示修改失败
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
